// webpack.base.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require("webpack");
const { buildOutPath, cndPath } = require("./base.config");

const isDev = process.env.NODE_ENV === "development";

module.exports = {
	// 打包文件出口
	output: {
		filename: "static/js/[name].js", // 每个输出js的名称
		clean: true, // webpack4需要配置clean-webpack-plugin删除dist文件，webpack5内置了。
	},
	module: {
		rules: [
			{
				test: /\.css$/, //匹配所有的 less 文件
				enforce: "pre",
				exclude: /node_modules/,
				use: [isDev ? "style-loader" : MiniCssExtractPlugin.loader, "css-loader", "postcss-loader"],
			},
			{
				test: /\.(png|jpg|jpeg|gif|svg)$/,
				type: "asset",
				parser: {
					//转base64的条件
					dataUrlCondition: {
						maxSize: 10 * 1024, // 10kb
					},
				},
				generator: {
					filename: "static/images/[name].[contenthash:6][ext]",
				},
			},
			{
				test: /\.(woff2?|eot|ttf|otf)$/, // 匹配字体图标文件
				type: "asset", // type选择asset
				parser: {
					dataUrlCondition: {
						maxSize: 10 * 1024, // 小于10kb转base64位
					},
				},
				generator: {
					filename: "static/fonts/[name].[contenthash:6][ext]", // 文件输出目录和命名
				},
			},
			{
				test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)$/, // 匹配媒体文件
				type: "asset", // type选择asset
				parser: {
					dataUrlCondition: {
						maxSize: 10 * 1024, // 小于10kb转base64位
					},
				},
				generator: {
					filename: "static/media/[name].[contenthash:6][ext]", // 文件输出目录和命名
				},
			},
		],
	},
	resolve: {
		extensions: [".js", ".tsx", ".ts"],
	},
	plugins: [],
	// 开启webpack持久化存储缓存
	cache: {
		type: "filesystem", // 使用文件缓存
	},
};
